'use strict';

angular.module('app')
    .controller('scenesController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state) {
            $scope.title = '流量场景包';
            $scope.searchParams = { };
            $scope.loading = false;

            $scope.search = function () {
                $scope.loading = true;
                // $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+ '/scenesPackage/readList',
                    data: $scope.searchParams
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                    $scope.selected = [];
                });
            };
            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $scope.selected = [];
                $.ajax({
                    url : $scope.app.project_name+ '/scenesPackage/readList',
                    data: $scope.searchParams
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            };

            $scope.search();

            $scope.delSearch = function() {
                // if(confirm('确定要执行此操作吗?')){
                $scope.alertMessage({
                    text:"确定做此操作吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
                    var delsfruit='0';
                    var leng=0;
                    var count=0;
                    var nocount=0;
                    angular.forEach($scope.pageInfo.records, function(value, key) {
                        // mediaId=mediaId+','+value.id;
                        // console.log(value);
                        if(value.checked){
                            if(value.scenesType==2){
                                nocount=nocount+1;
                            }else {
                                count=count+1;
                            }
                        }
                    });

                    $('input[name="subCheckBox"]:checked').each(function() {
                        var sfruit = $(this).val();
                        leng=sfruit.length;
                        delsfruit=delsfruit+","+sfruit;
                    });
                    if(delsfruit.length>2){
                        delsfruit=delsfruit.substring(2);
                        $.ajax({
                            url : $scope.app.project_name+ '/scenesPackage/delToolManageCrt',
                            data: {'ids':delsfruit}
                        }).then(function(result) {
                            $scope.loading = false;
                            if (result.httpCode == 200) {
                                $scope.alertModal({"content":"公共删除成功"+count+"条,用户自定义"+nocount+"条未删除"});
                                $scope.selected = [];
                                // }
                            } else {
                                $scope.msg = result.msg;
                            }
                            $scope.$apply();
                        });

                    }else {
                        $scope.alertModal({"content":"未选择删除行"});
                    }
                    $scope.search();
                })
                // }
            };

            $scope.addSearch = function() {
                $scope.searchParams= {};
                $('#addScenes').modal('show',true);

                $scope.search();
            };

            $scope.disableItem = function(id, enable) {

            };

            $scope.selected = [];
            // 全选操作
            $scope.selectAll = function ($event) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                var action = (checkbox.checked ? 'add' : 'remove');
                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    var contact = $scope.pageInfo.records[i];
                    updateSelected(action, contact);
                }
            };

            $scope.updateSelection = function ($event, contact) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }
                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };


            var updateSelected = function (action, contact) {
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }

                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };

            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };


            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };
        } ]);